package com.xhc.server.service;

import com.xhc.server.domain.CourseContentFile;
import com.xhc.server.domain.CourseContentFileExample;
import com.xhc.server.dto.CourseContentFileDto;
import com.xhc.server.mapper.CourseContentFileMapper;
import com.xhc.server.util.CopyUtil;
import com.xhc.server.util.UuidUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version 1.0
 * @Author: XHC
 * @Date: 2023-12-20-0:09
 * @Description: 吃番茄不吃番茄酱
 * @注释:
 */
@Service
public class CourseContentFileService {

    @Resource
    private CourseContentFileMapper courseContentFileMapper;

    /**
     * 列表查询
     * @param courseId
     */
    public List<CourseContentFileDto> list(String courseId){
        CourseContentFileExample example = new CourseContentFileExample();
        CourseContentFileExample.Criteria criteria = example.createCriteria();
        criteria.andCourseIdEqualTo(courseId);
        List<CourseContentFile> fileList = courseContentFileMapper.selectByExample(example);
        return CopyUtil.copyList(fileList, CourseContentFileDto.class);

    }

    /**
     * 保存，id有值时更新，无值时新增
     * @param courseContentFileDto
     */
    public void save(CourseContentFileDto courseContentFileDto){
        CourseContentFile courseContentFile = CopyUtil.copy(courseContentFileDto, CourseContentFile.class);
        if (StringUtils.isEmpty(courseContentFileDto.getId())){
            this.insert(courseContentFile);
        }else {
            this.update(courseContentFile);
        }
    }

    /**
     * 新增
     * @param courseContentFile
     */
    private void insert(CourseContentFile courseContentFile){
        courseContentFile.setId(UuidUtil.getShortUuid());
        courseContentFileMapper.insert(courseContentFile);
    }

    /**
     * 更新
     * @param courseContentFile
     */
    private void update(CourseContentFile courseContentFile){
        courseContentFileMapper.updateByPrimaryKey(courseContentFile);

    }

    /**
     * 删除
     * @param id
     */
    public void delete(String id) {
        courseContentFileMapper.deleteByPrimaryKey(id);
    }


}